package com.pyb.module.live.fragment;

import android.util.Log;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.pyb.java.basemodule.BaseFragment;
import com.pyb.java.commonutil.constant.RouterFragmentPath;
import com.pyb.java.commonutil.eventbus.EventBusKeys;
import com.pyb.java.commonutil.eventbus.EventBusMessage;
import com.pyb.java.commonutil.eventbus.EventBusUtils;
import com.pyb.module.live.R;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;

/**
 * 首页考友圈视频页面fragment
 */
@Route(path = RouterFragmentPath.Live.PAGER_HOME_LIVE_FM)
public class HomeLiveFragment extends BaseFragment {

    private SlidingTabLayout homeVideoTabs;
    private ViewPager viewPager;
    private String[] mTitles;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private int currentPos = 1;

    @Override
    protected void initView(View view) {
        homeVideoTabs = view.findViewById(R.id.homeVideoTabs);
        viewPager = view.findViewById(R.id.videoHomeViewPager);
        initFragments();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBusUtils.unRegister(this);
    }

    @Subscribe
    public void changeTabsTextColor(EventBusMessage messageEvent) {
        if (messageEvent.getKey() == EventBusKeys.CHANGE_TABS_COLOR_NO_DATA) {
            if (homeVideoTabs == null) return;
            homeVideoTabs.setTextSelectColor(ContextCompat.getColor(getActivity(), R.color.live_color_33333));
            homeVideoTabs.setTextUnselectColor(ContextCompat.getColor(getActivity(), R.color.live_color_999999));
        }
        if (messageEvent.getKey() == EventBusKeys.CHANGE_TABS_COLOR_HAS_DATA) {
            if (homeVideoTabs == null) return;
            homeVideoTabs.setTextSelectColor(ContextCompat.getColor(getActivity(), R.color.live_white));
            homeVideoTabs.setTextUnselectColor(ContextCompat.getColor(getActivity(), R.color.live_color_D1D1D1));
        }
    }

    private void initFragments() {
        EventBusUtils.register(this);
        mFragments.clear();
        mFragments.add(new LiveFragment());
        mFragments.add(new VideoItemFragment());
        mFragments.add(new VideoItemFragment());
        mTitles = new String[]{"直播", "关注", "推荐"};
        viewPager.setAdapter(new MyPagerAdapter(getChildFragmentManager()));
        viewPager.setOffscreenPageLimit(mFragments.size());
        homeVideoTabs.setViewPager(viewPager);
        homeVideoTabs.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                currentPos = position;
                Log.i("inffs", "onPageSelected: " + position);
                homeVideoTabs.setCurrentTab(position);
                for (int i = 0; i < mFragments.size(); i++) {
                    VideoBaseFragment baseFragment = (VideoBaseFragment) mFragments.get(i);
                    if (currentPos == i) {
                        baseFragment.onPlayVideoResume();
                    } else {
                        baseFragment.onPlayVideoPause();
                    }

                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        viewPager.setCurrentItem(1);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.live_home_video_fragment;
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i("inffs", "111onResume: " + isHidden());
        VideoBaseFragment baseFragment = (VideoBaseFragment) mFragments.get(currentPos);
        if (baseFragment != null && !isHidden()) baseFragment.onPlayVideoResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        for (int i = 0; i < mFragments.size(); i++) {
            VideoBaseFragment baseFragment = (VideoBaseFragment) mFragments.get(i);
            if (baseFragment != null) baseFragment.onPlayVideoPause();
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        Log.i("inffs", "onHiddenChanged: " + hidden);
        if (hidden && isResumed()) {
            for (int i = 0; i < mFragments.size(); i++) {
                VideoBaseFragment baseFragment = (VideoBaseFragment) mFragments.get(i);
                baseFragment.onPlayVideoPause();
            }
        }
        if (!hidden && isResumed()) {
            VideoBaseFragment baseFragment = (VideoBaseFragment) mFragments.get(currentPos);
            baseFragment.onPlayVideoResume();
        }
    }
}
